package com.zwkj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zwkj.domain.WFileT;
import com.zwkj.pojo.vo.FileVo;
import com.zwkj.pojo.vo.WFileVersionTVo;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zwkj.domain.WFileVersionT;
import com.zwkj.service.IWFileVersionTService;
import com.zwkj.mapper.WFileVersionTMapper;
import org.springframework.stereotype.Service;

/**
 * @author 小间隙
 * @description 针对表【w_file_version_t】的数据库操作Service实现
 * @createDate 2023-12-11 11:32:44
 */
@Service
public class WFileVersionTServiceImpl extends ServiceImpl<WFileVersionTMapper, WFileVersionT>
        implements IWFileVersionTService {

    @Autowired
    private IWFileVersionTService wFileVersionTService;


    @Override
    public void addWFileVersionT(WFileVersionTVo wFileVersionTVo) {
        WFileVersionT wFileVersionT = new WFileVersionT();
        BeanUtils.copyProperties(wFileVersionTVo, wFileVersionT);

        wFileVersionTService.save(wFileVersionT);
    }

    @Override
    public IPage<WFileVersionTVo> getWFileVersionTPageList(IPage<WFileVersionTVo> page, WFileVersionTVo wFileVersionTVo) {
        return this.baseMapper.getWFileVersionTPageList(page, wFileVersionTVo);
    }


    @Override
    public List<WFileVersionTVo> getWFileVersionTList(WFileVersionTVo wFileVersionTVo) {
        return this.baseMapper.getWFileVersionTList(wFileVersionTVo);

    }

    @Override
    public WFileVersionT getWFileVersionTById(String id) {
        return wFileVersionTService.getById(id);
    }


    @Override
    public void delWFileVersionT(String id) {
        wFileVersionTService.removeById(id);
    }

    @Override
    public List<FileVo> fileVersions(String fileId, int offset, int limit) {
        List<WFileVersionT> versionTList = wFileVersionTService.list(new QueryWrapper<WFileVersionT>().eq("file_id", fileId).orderByDesc("version"));
        List<FileVo> fileVoList = new ArrayList<>();
        for (WFileVersionT v : versionTList) {
            FileVo fileVo = new FileVo();
            fileVo.setId(fileId);
            fileVo.setName(v.getName());
            fileVo.setVersion(v.getVersion());
            fileVo.setSize(Math.toIntExact(v.getSize()));
            fileVo.setCreateTime(Math.toIntExact(v.getCreateTime().toEpochSecond(ZoneOffset.UTC)));
            fileVo.setModifyTime(Math.toIntExact(v.getModifyTime().toEpochSecond(ZoneOffset.UTC)));
            fileVo.setCreatorId(v.getCreator());
            fileVo.setModifierId(v.getModifier());
            fileVoList.add(fileVo);
        }
        return fileVoList;
    }

    @Override
    public FileVo fileVersionByVersion(String fileId, int version) {
        List<WFileVersionT> versionTList = wFileVersionTService.list(new QueryWrapper<WFileVersionT>().eq("file_id", fileId).eq("version", version));

        if (versionTList.size() > 0) {
            FileVo fileVo = new FileVo();
            fileVo.setId(fileId);
            fileVo.setName(versionTList.get(0).getName());
            fileVo.setVersion(versionTList.get(0).getVersion());
            fileVo.setSize(Math.toIntExact(versionTList.get(0).getSize()));
            fileVo.setCreateTime(Math.toIntExact(versionTList.get(0).getCreateTime().toEpochSecond(ZoneOffset.UTC)));
            fileVo.setModifyTime(Math.toIntExact(versionTList.get(0).getModifyTime().toEpochSecond(ZoneOffset.UTC)));
            fileVo.setCreatorId(versionTList.get(0).getCreator());
            fileVo.setModifierId(versionTList.get(0).getModifier());
            return fileVo;
        }
        return null;
    }

    @Override
    public FileVo fileVersionDownload(String fileId, int version) {
        List<WFileVersionT> versionTList = wFileVersionTService.list(new QueryWrapper<WFileVersionT>().eq("file_id", fileId).eq("version", version));
        if (versionTList.size() > 0) {
            FileVo fileVo = new FileVo();
            fileVo.setId(fileId);
            fileVo.setName(versionTList.get(0).getName());
            fileVo.setVersion(versionTList.get(0).getVersion());
            fileVo.setUrl(versionTList.get(0).getDownloadUrl());
            return fileVo;
        }
        return null;
    }


    @Override
    public void updateWFileVersionT(WFileVersionTVo wFileVersionTVo) {
        WFileVersionT wFileVersionT = new WFileVersionT();
        BeanUtils.copyProperties(wFileVersionTVo, wFileVersionT);
        wFileVersionTService.updateById(wFileVersionT);
    }
}




